﻿//-----------------------------------------------------------------------
// <copyright file="FileSystemContentProvider.cs" author="Artak Mkrtchyan">
//     Creative Commons - GNU General Public License (http://creativecommons.org/licenses/GPL/2.0/
// </copyright>
// <author>Artak Mkrtchyan</author>
// <email>mkArtak@gmail.com</email>
// <date>19/03/2013</date>

using ConsoleHoster.Common.Model;
using System;
using System.Collections.Generic;
using System.IO;

namespace ConsoleHoster.Common.Utilities.IO
{
	public class FileSystemContentProvider : IFileSystemContentProvider
	{
		public FileSystemContentProvider()
		{

		}

		public string[] GetFolders(string argFolder, string argPattern)
		{
			string[] tmpResult = Directory.GetDirectories(argFolder, GetNormalizedPattern(argPattern), SearchOption.TopDirectoryOnly);

			return tmpResult;
		}

		public string[] GetFiles(string argFolder)
		{
			string[] tmpResult = Directory.GetFiles(argFolder);

			return tmpResult;
		}

		public string[] GetAllItems(string argFolder, string argPattern)
		{
			string[] tmpResult = Directory.GetFileSystemEntries(argFolder, GetNormalizedPattern(argPattern), SearchOption.TopDirectoryOnly);

			return tmpResult;
		}

		public IEnumerable<string> EnumerateFiles(string argPath)
		{
			return Directory.EnumerateFiles(argPath);
		}

		public IEnumerable<string> EnumerateFolders(string argPath)
		{
			return Directory.EnumerateDirectories(argPath);
		}

		private static string GetNormalizedPattern(string argPattern)
		{
			string tmpResult = argPattern ?? String.Empty;
			if (!tmpResult.EndsWith("*"))
			{
				tmpResult = tmpResult + "*";
			}
			return tmpResult;
		}

		public IEnumerable<DriveInfo> GetAvailableDrives()
		{
			return DriveInfo.GetDrives();
		}

		public string GetCurrentDirectory()
		{
			return Directory.GetCurrentDirectory();
		}

		public bool DirectoryExists(string argPath)
		{
			return Directory.Exists(argPath);
		}

		public bool FileExists(string argPath)
		{
			return File.Exists(argPath);
		}

		public void CreateDirectory(string argPath)
		{
			Directory.CreateDirectory(argPath);
		}

		public void DeleteDirectory(string argPath, bool argRecursive)
		{
			Directory.Delete(argPath, argRecursive);
		}

		public void DeleteFile(string argPath)
		{
			File.Delete(argPath);
		}

		public void CopyFile(string argSourceFile, string argDestinationFile, bool argOverwrite)
		{
			File.Copy(argSourceFile, argDestinationFile, argOverwrite);
		}

		public string GetFileDirectory(string argFilePath)
		{
			return new FileInfo(argFilePath).Directory.FullName;
		}

		public string GetRandomFolderName()
		{
			return Guid.NewGuid().ToString();
		}

		public string GetTempLocationInCurrentFolderForFile(string argFolder, string argFilename)
		{
			if (!this.DirectoryExists(argFolder))
			{
				this.CreateDirectory(argFolder);
			}

			return Path.Combine(this.GetCurrentDirectory(), argFolder, argFilename);
		}

		public void ExportShortcut(IWindowsShortcut argShortcut, string argPath)
		{
			ValidationManager.RequireArgumentNotNull(argShortcut, "argShortcut");

			argShortcut.Save(argPath);
		}
	}
}
